package com.sap.jcp.sm.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.sap.jcp.sm.domain.Shift;
import com.sap.jcp.sm.domain.Track;
import com.sap.jcp.sm.domain.User;
import com.sap.jcp.sm.domain.UserTimeline;

public class MappingUtil {

	public static Map<String, UserTimeline> userTimelineMapper (List<UserTimeline> userTimelineList) {
		Map<String, UserTimeline> userTimelineMap = new HashMap<String,UserTimeline>();
		if (userTimelineList != null) {
			Iterator<UserTimeline> iter = (Iterator<UserTimeline>) userTimelineList.iterator();
			while (iter.hasNext()) {
				UserTimeline userTimelineItem = iter.next();
				userTimelineMap.put(getKeyString(userTimelineItem), userTimelineItem);
			}
		}
		return userTimelineMap;
	}
	
	public static String getKeyString(UserTimeline userTimelineItem) {
		if (userTimelineItem != null) {
			String dateString = userTimelineItem.getDay() + "-" + userTimelineItem.getMonth() + "-" + userTimelineItem.getYear();
			SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
			try {
				Date currentWeekDate = sdf.parse(dateString);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(currentWeekDate);
				Date dateConverted = calendar.getTime();
				String dateStringFormatted = sdf.format(dateConverted);
				String returnVal = userTimelineItem.getUser() + "_" + dateStringFormatted;
				return returnVal;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static Map<String, User> convertUserListToMap (List<User> userList) {
		
		Map<String, User> userMap = new LinkedHashMap<String, User>();
		if (userList != null) {
			for (User user : userList) {
				userMap.put(user.getId().toString(), user);
			}
		}
		return userMap;
	}
	
	public static Map<String, Track> convertTrackListToMap (List<Track> trackList) {
		
		Map<String, Track> trackMap = new HashMap<String, Track>();
		if (trackList != null) {
			Iterator<Track> iter = trackList.iterator();
			while (iter.hasNext()) {
				Track track = (Track) iter.next();
				trackMap.put(track.getTrackId().toString(), track);
			}
		}
		return trackMap;
	}
	
	public static Map<String, Shift> convertShiftListToMap (List<Shift> shiftList) {
		
		Map<String, Shift> shiftMap = new HashMap<String, Shift>();
		if (shiftList != null) {
			Iterator<Shift> iter = shiftList.iterator();
			while (iter.hasNext()) {
				Shift shift = (Shift) iter.next();
				shiftMap.put(shift.getShiftId().toString(), shift);
			}
		}
		return shiftMap;
	}
	
	public static Map<String, List<UserTimeline>> userTimelineMapperForSchedule (List<UserTimeline> userTimelineList) {
		Map<String, List<UserTimeline>> userTimelineMap = new HashMap<String,List<UserTimeline>>();
		if (userTimelineList != null) {
			Iterator<UserTimeline> iter = (Iterator<UserTimeline>) userTimelineList.iterator();
			while (iter.hasNext()) {
				UserTimeline userTimelineItem = iter.next();
				String dayKey = getDateString(userTimelineItem);
				if (userTimelineMap.get(dayKey) != null) {
					userTimelineMap.get(dayKey).add(userTimelineItem);
				} else {
					List<UserTimeline> userTimelineItemList = new ArrayList<UserTimeline>();
					userTimelineItemList.add(userTimelineItem);
					userTimelineMap.put(dayKey, userTimelineItemList);
				}
			}
		}
		return userTimelineMap;
	}
	
	public static String getDateString(UserTimeline userTimelineItem) {
		if (userTimelineItem != null) {
			String dateString = userTimelineItem.getDay() + "-" + userTimelineItem.getMonth() + "-" + userTimelineItem.getYear();
			SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
			try {
				Date currentWeekDate = sdf.parse(dateString);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(currentWeekDate);
				Date dateConverted = calendar.getTime();
				String dateStringFormatted = sdf.format(dateConverted);
				return dateStringFormatted;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
